For Loop Explained | Control Flow, track, Context Variables
@for in Angular
@foris a new control flow statement in Angular.- It is used for looping (iteration) inside HTML templates.
- It replaces the older
*ngForsyntax. - It is used only in template (HTML) files, not in TypeScript files.
When the instructor says “for”, it refers to the @for control flow statement.Loops Be Used in Angular
There are two places where looping can be done:
- TypeScript file → normal JavaScript loops (
for,map, etc.) - HTML template →
@forcontrol flow
@for should only be used in HTML, never in .ts files.
Why Use Signals with @for?
- Angular now recommends using Signals instead of normal properties
- Signals:
- Are faster
- Have better performance
- Handle DOM updates efficiently
- From Angular 19+ (and future versions), Signals are stable and preferred
Creating a Simple Array with Signals
TypeScript Code
export class App {
users = signal(["Anil", "Sam", "Peter", "Bruce"]);
}
usersis a signal- It contains an array of strings
- Signals must be accessed using
users()
Using @for with a Simple Array
HTML Code
<ul>
@for (user of users(); track user) {
<li>{{ user }}</li>
}
</ul>
Explanation
user→ loop variableusers()→ signal valuetrack user→ unique value for tracking items
Works best when array values are unique
Why track is Important?
trackhelps Angular efficiently update the DOM- It must be unique
- Avoid using
$indexfor tracking
Why NOT $index?
- Index changes when:
- Items are added
- Items are removed
- This causes incorrect DOM updates
Best Practice
- Use a unique value like:
- ID
- Unique string
Looping Over an Array of Objects
TypeScript Code
usersDetail = signal([
{ id: 1, name: "Anil", surname: "Sidhu", email: "anil@test.com" },
{ id: 2, name: "Sam", surname: "Singh", email: "sam@test.com" },
{ id: 3, name: "Peter", surname: "Parker", email: "peter@test.com" },
{ id: 4, name: "Bruce", surname: "Wayen", email: "bruce@test.com" }
]);
- Each object has a unique
id - Ideal for
trackusage
Using @for with Objects
HTML Code
<ul>
@for (user of usersDetail(); track user.id) {
<li>
{{ user.name }} {{ user.surname }}
</li>
}
</ul>
Explanation
track user.id→ best practice- Ensures stable DOM updates
Contextual Variables in @for
Angular provides built-in contextual variables inside @for.
$index | Current index (starts from 0) |
$count | Total number of items |
$first | True for first item |
$last | True for last item |
$even | True for even index |
$odd | True for odd index |
Using $index for Display (Not Tracking)
<span>{{ $index + 1 }}</span>
- Used for serial numbers
- Add
+1if numbering should start from 1
Using $first and $last
Example
@if ($first) {
<span style="color:red">Admin</span>
}
@if ($last) {
<span style="color:orange">Guest</span>
}
Use Case
- First user → Admin
- Last user → Guest
Styling Even and Odd Rows
<li [style.backgroundColor]="$even ? '#ccc' : '#fff'">
$even→ even rows$odd→ odd rows- Useful for table-like UI
Showing Total Count at the End
@if ($count == $index + 1) {
<b>Total Users {{ $count }}</b>
}
Explanation
- Runs only on the last item
- Displays total number of users